package com.bangmai.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;



/**
 * HHTPЭ����ز���������
 * @author dellpc
 *
 */
public class HttpRequestUtils {
	
	
	/**
	 * ��֧����˾�ς����Ĳ��@�û؈̈���
	 * @param param
	 * @param posturl
	 * @return
	 */

	public static byte[] post(byte[] param, String posturl,String localfilepath) {
		try {
			URL url = new URL(posturl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			connection.setConnectTimeout(60000);
			connection.setReadTimeout(600000);
			connection.setRequestProperty("Accept-Charset", "utf-8");
			connection.setRequestProperty("contentType", "utf-8");
			connection.connect();

			OutputStream os = connection.getOutputStream();
			os.write(param);
			os.flush();
			os.close();

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
			int len = 0;
			byte[] buf = new byte[1024];
			while ((len = bis.read(buf)) != -1) {
				baos.write(buf, 0, len);
			}
			byte[] bufferByte = new byte[256];

			int l = -1;

			int downloadSize = 0;

			while ((l = bis.read(bufferByte)) > -1) {

				downloadSize += l;

				baos.write(bufferByte, 0, l);

				baos.flush();

			}
			String xml = baos.toString();
			File file2 = new File(localfilepath);
			FileWriter writer = new FileWriter(file2);
			writer.write(xml);
			writer.flush();
			writer.close();
			bis.close();
			connection.disconnect();
			return baos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
